package org.apache.commons.graph.algorithms.elo

import org.apache.commons.graph.DirectedGraph
import kotlin.math.pow

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
 * TODO Fill me!!
 *
 * @param <P>
</P> */
interface KFactorBuilder<P> {
    /**
     * TODO Fill me!!
     */
    fun withDefaultKFactor()

    /**
     * TODO Fill me!!
     */
    fun withKFactor(kFactor: Int)
}


internal class DefaultKFactorBuilder<P>(
    private val tournamentGraph: DirectedGraph<P, GameResult>,
    private val playerRanking: PlayersRank<P>
) : KFactorBuilder<P> {

    private fun calculateQFactor(player: P): Double {
        val ranking = playerRanking.getRanking(player)
        return DEFAULT_POW_BASE.pow(ranking / DEFAULT_DIVISOR)
    }

    private fun evaluateMatch(playerA: P, gameResult: GameResult, playerB: P, kFactor: Int): Boolean {
        val qA = calculateQFactor(playerA)
        val qB = calculateQFactor(playerB)

        val eA = calculateEFactor(qA, qB)
        val eB = calculateEFactor(qB, qA)

        val sA: Double
        val sB: Double
        when (gameResult) {
            GameResult.WIN -> {
                sA = 1.0
                sB = 0.0
            }

            GameResult.DRAW -> {
                val number = 0.5
                sA = number
                sB = number
            }

            else -> throw IllegalArgumentException("Input GameResult not accepted")
        }

        updateRanking(playerA, kFactor.toDouble(), sA, eA)
        updateRanking(playerB, kFactor.toDouble(), sB, eB)
        return true
    }

    private fun updateRanking(player: P, kFactor: Double, sFactor: Double, eFactor: Double) {
        val newRanking = playerRanking.getRanking(player) + (kFactor * (sFactor - eFactor))
        playerRanking.updateRanking(player, newRanking)
    }

    override fun withDefaultKFactor() {
        withKFactor(DEFAULT_K_FACTOR)
    }

    override fun withKFactor(kFactor: Int) {
        // TODO find a way to improve performances, this impl is just a spike
        for (player in tournamentGraph.vertices) {
            for (opponent in tournamentGraph.getOutbound(player)) {
                val gameResult = tournamentGraph.getEdge(player, opponent)
                evaluateMatch(player, gameResult!!, opponent, kFactor)
            }
        }
    }

    companion object {
        private const val DEFAULT_POW_BASE = 10.0

        private const val DEFAULT_DIVISOR = 400.0

        private const val DEFAULT_K_FACTOR = 32

        private fun calculateEFactor(qA: Double, qB: Double): Double {
            return qA / (qA + qB)
        }
    }
}
